package com.qf.emp.dao.impl;

import com.qf.emp.dao.IDeptDao;
import com.qf.emp.entity.Dept;
import com.qf.emp.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class DeptDaoImpl implements IDeptDao {

    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    @Override
    public Integer insert(Dept dept) throws Exception {
        String sql = "insert into t_dept(dname,loc) values(?,?)";
        return qr.update(sql, dept.getDname(),dept.getLoc());
    }

    @Override
    public Dept selectById(Integer id) throws Exception {
        String sql = "select * from t_dept where deptno = ?";
        Dept dept = qr.query(sql, new BeanHandler<Dept>(Dept.class), id);
        return dept;
    }

    @Override
    public Integer updateById(Dept dept) throws Exception {
        String sql = "update t_dept set dname = ?,loc = ? where deptno = ?";
        return qr.update(sql, dept.getDname(),dept.getLoc(),dept.getDeptno());
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        String sql = "delete from t_dept where deptno = ?";
        return qr.update(sql, id);
    }

    @Override
    public List<Dept> selectList() throws Exception {
        String sql = "select * from t_dept";
        return qr.query(sql, new BeanListHandler<Dept>(Dept.class));
    }
}
